package com.huika.dealontop.control.shops.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.huika.dealontop.R;
import com.huika.dealontop.control.shops.fragment.OtherProductListFragment;
import com.huika.dealontop.control.shops.fragment.ProductEvaluationListFragment;
import com.huika.dealontop.control.shops.fragment.ProductNoticeFragment;
import com.huika.dealontop.support.domain.ProductDetailsBean;
import com.huika.dealontop.utils.UIUtils;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Author: Victor Wu <br/>
 * Email: victor-wu*foxmail.com <br/>
 * Time: 2015/11/23 11:51 <br/>
 * Description: 商品详情 分栏 页面适配器
 */
public class ProductDetailFootAdapter extends FragmentPagerAdapter {

    private ProductDetailsBean productDetailsBean;

    private Map<Integer, Fragment> tabFragments = new LinkedHashMap<>();

    private String[] mTitles;

    public ProductDetailFootAdapter(FragmentManager fm) {
        super(fm);

        // 页面标题
        mTitles = UIUtils.getStringArray(R.array.product_foot_select_titles);
    }

    @Override
    public Fragment getItem(int i) {
        Fragment fragment = tabFragments.get(i);
        if (fragment == null) {
            switch (i) {
                case 0:
                    //其他商品列表
                    fragment = new OtherProductListFragment();
                    ((OtherProductListFragment) fragment).setProductDetails(this.productDetailsBean.getProduct());
                    break;
                case 1:
                    //商品评价
                    fragment = new ProductEvaluationListFragment();
                    ((ProductEvaluationListFragment) fragment).setProductDetails(this.productDetailsBean.getProduct());
                    break;
                case 2:
                    //买家须知
                    fragment = new ProductNoticeFragment();
                    ((ProductNoticeFragment) fragment).setProductDetails(this.productDetailsBean.getProduct());
                    break;
            }
            tabFragments.put(i, fragment);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        if (mTitles != null) {
            return mTitles.length;
        }
        return 0;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (mTitles != null) {
            return mTitles[position];
        }
        return super.getPageTitle(position);
    }

    public void setProductDetails(ProductDetailsBean productDetails) {
        this.productDetailsBean = productDetails;
    }
}
